<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="../../pub/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>
<%@include file="../../pub/resource.jsp"%>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.easyPaginate.js"></script>
<style type="text/css">
body {
	padding-bottom: 40px;
}

.sidebar-nav {
	padding: 9px 0;
}

@media ( max-width : 980px) {
	/* Enable use of floated navbar text */
	.navbar-text.pull-right {
		float: none;
		padding-left: 5px;
		padding-right: 5px;
	}
}

#paginatio .btn {
	margin: 4px;
}

#paginatio .dots {
	margin: 4px;
	font-size: 20px;
	text-decoration: none;
}
</style>
</head>
<body>
	<form id="form1" class="form-inline definewidth m20"
		action="accountIndex.do" method="post">
		用户名： <input type="text" name="username" id="username"
			class="abc input-default" placeholder="用户名" value="${username }">&nbsp;&nbsp;
		<shiro:hasPermission name="用户管理:查询">
			<button type="submit" class="btn btn-primary">查询</button>
		</shiro:hasPermission>
		<shiro:hasPermission name="用户管理:添加">
			&nbsp;&nbsp;<button type="button" class="btn btn-success" id="addnew">新增用户</button>
		</shiro:hasPermission>
		<shiro:hasPermission name="用户管理:删除">
			&nbsp;&nbsp;<input type="button" class="btn btn-danger"
				id="delAccount" value="删除用户" />
		</shiro:hasPermission>
		<shiro:hasPermission name="用户管理:权限管理">
			&nbsp;&nbsp;<input type="button" value="权限管理" id="btnFun"
				name="btnFun" class="btn btn-info" />
		</shiro:hasPermission>
		<input type="hidden" id="pageSize" name="pageSize" value="15">
		<input type="hidden" id="curPage" name="curPage" value="1">
	</form>
	<table class="table table-bordered table-hover definewidth m10">
		<thead>
			<tr>
				<th style="width: 60px;"><input type="checkbox" id="cBoxAll"
					name="cBoxAll" /></th>
				<th>序号</th>
				<th>用户名</th>
				<th>昵称</th>
				<th>姓名</th>
				<th>性别</th>
				<th>年龄</th>
				<th>部门</th>
				<th>职位</th>
				<th>角色</th>
				<th>状态</th>
				<th>最后一次登录时间</th>
				<th>操作</th>
			</tr>
		</thead>
		<c:forEach begin="0" step="1" items="${list }" var="gl"
			varStatus="var">
			<tr>
				<td><input type="checkbox" name="cBoxItem" value="${gl.id }" /></td>
				<td>${(curPage-1)*pageSize+var.count}</td>
				<td>${gl.username }</td>
				<td>${gl.nickName }</td>
				<td>${gl.name }</td>
				<td><c:if test="${gl.sex==1 }">男</c:if> <c:if
						test="${gl.sex==0 }">女</c:if></td>
				<td>${gl.age }</td>
				<td>${gl.department }</td>
				<td>${gl.position }</td>
				<td>${gl.role }</td>
				<td><c:if test="${gl.inuse==1 }">正常</c:if> <c:if
						test="${gl.inuse!=1 }">
						<font color="red">停用</font>
					</c:if></td>
				<td><fmt:formatDate value="${gl.lastLoginTime }"
						pattern="yyyy-MM-dd HH:mm:ss" /></td>
				<td><shiro:hasPermission name="用户管理:禁用">
						<c:if test="${gl.inuse==1 }">
							<a href="javascript:void(0)" onclick="changeStat(0,${gl.id})">禁用</a>
						</c:if>
					</shiro:hasPermission> <shiro:hasPermission name="用户管理:启用">
						<c:if test="${gl.inuse!=1 }">
							<a href="javascript:void(0)" onclick="changeStat(1,${gl.id})">启用</a>
						</c:if>
					</shiro:hasPermission> <shiro:hasPermission name="用户管理:编辑">
						<a href="showEdit.do?id=${gl.id }">编辑</a>
					</shiro:hasPermission></td>
			</tr>
		</c:forEach>
		<c:if test="${empty list }">
			<tr>
				<td colspan="13">暂无数据</td>
			</tr>
		</c:if>
	</table>
	<div id="paginatio" style="float: right; margin-right: 25px;"></div>
</body>
<script type="text/javascript">
	$(function() {
		$("#addnew").click(function() {
			window.location.href = "showAdd.do";
		});

		$("#cBoxAll").click(
				function() {
					$("[name='cBoxItem']:checkbox")
							.attr(
									"checked",
									$(this).attr("checked") == "checked" ? true
											: false);
				});
		$("#delAccount").click(
				function() {
					if (confirm("你确定要进行删除操作？")) {
						var str = "";
						$("[name='cBoxItem']:checkbox:checked").each(
								function(index, element) {
									str += $(this).val() + ",";
								})
						if (str != "") {
							str = str.substring(0, str.length - 1);
						}
						if (str == "") {
							alertWin("请选中要删除的项！","warning");
							return;
						}
						$.ajax({
							url : "del.do",
							type : "post",
							data : {
								ids : str
							},
							dataType : "text",
							success : function(data) {
								if (data == "success") {
									alertWinWithRefresh("删除成功！","succeed");
								}
							}
						});
					}
				});
		$("#btnFun").click(function(){
			if($("[name='cBoxItem']:checkbox:checked").size()>1){
				alertWin("每次只能为一个用户设定权限！","warning");
				return false;
			}
			var accountId=$("[name='cBoxItem']:checkbox:checked").val();
			if(accountId==undefined||accountId==""){
				alertWin("请选择一个用户！","warning");
				return false;
			}
				
				$.ajax({
					url:"menuFunc.do",
					type:"post",
					data:{},
					dataType:"json",
					success:function(data){
						var content="<table class='table table-bordered table-hover definewidth m10' style='width:550px;'>";
						for(var i=0 in data){
							content+="<tr><td class='tableright' style='width:80px;'>"+data[i].name+"：</td><td class='tableleft'>";
							for(var j=0 in data[i].func){
								content+="<input type='checkbox' name='funcs' value='"+data[i].func[j].id+"'>&nbsp;"+data[i].func[j].name+"&nbsp;&nbsp;";
							}
							content+="</td></tr>";
						}
						content+="</table>"
						
						currentWin.$.dialog({
							title:"权限管理",
							content:"<div style='height:400px;overflow:auto;width:580px;'>"+content+"</div>",
							lock: true,
							path : rootPath+"/css",
							width:"600px",
							init:getAccountFuncs,
							ok:function(){
								var selected="";
								currentWin.$(":checkbox[name='funcs']:checked").each(function(index,element){
									selected+=$(this).val()+",";
								})
								if(selected==""){
									alertWin("请选择相应的权限！","warning");
									return false;
								}
								$.ajax({
									url:"addFuncs.do",
									type:"post",
									data:{
										accountId:accountId,
										funcs:selected
									},
									dataType:"text",
									success:function(data){
										if(data=="success"){
											alertWin("操作成功！","succeed");
										}
									}
								});
							},
							okVal:"添加",
							cancel:true,
							cancelVal:"关闭"
						});
					}
				});
		});
	})
	
	var getAccountFuncs=function(){
		currentWin.$(":checkbox[name='funcs']").removeAttr("checked");
		var accountId=$("[name='cBoxItem']:checkbox:checked").val();
		$.ajax({
			url:"getFuncs.do",
			type:"post",
			data:{
				accountId:accountId
			},
			dataType:"text",
			success:function(data){
				var roles=data.split(",");
				currentWin.$(":checkbox[name='funcs']").each(function(index,element){
					for(var i in roles){
						if(roles[i]==$(this).val()){
							$(this).attr("checked","checked");
							break;
						}
					}
				});
			}
		});
	}

	$("#paginatio").easyPaginate({
		items : "${total}",
		itemsOnPage : $("#pageSize").val(),
		currentPage:"${curPage}",
		onClickcallback : function(page) {
			$("#curPage").val(page);
			$("#form1").submit();
		}
	});
	
	function changeStat(stat,id){
		$.ajax({
			url:"changeStat.do",
			type:"post",
			data:{
				stat:stat,
				accountId:id
			},
			dataType:"text",
			success:function(data){
				if(data=="success"){
					alertWin("操作成功！","succeed");
					window.location=window.location.href;
				}
			}
		});
	}
</script>
</html>